const { expect, use } = require('chai');
const { BigNumber } = require('ethers');
const { deployContract, MockProvider, solidity } = require('ethereum-waffle');
const { ethers, upgrades } = require("hardhat");

use(solidity);

describe("Test Lottery.randomNFT Interface", function () {
    var OWNER;
    var ADDR1;

    var BBoxSwap;
    var StruggleDog;
    var BlindBox;
    var ERC20;
    var Lottery;

    var BlindBoxRarityPR1 = [{name: "SR", PR: 2000, enumId: 1, totalSupply:10, issued:0 },
                            {name: "SSR", PR: 3000, enumId: 2, totalSupply:10, issued:0 },
                            {name: "UR", PR: 4000, enumId: 3, totalSupply:11, issued:0 }
                            ];

    var BlindBoxRarityPR2 = [{name: "SR", PR: 1200, enumId: 1, totalSupply:1, issued:0 },
                            {name: "SSR", PR: 2900, enumId: 2, totalSupply:1, issued:0 },
                            {name: "UR", PR: 3000, enumId: 3, totalSupply:10, issued:0 }
                            ];




    // 奋斗狗NFT合约、盲盒合约、盲盒商城合约、开盲盒随机合约
    beforeEach(async () => {
        [OWNER, ADDR1] = await ethers.getSigners(); // 初始化地址参数
        const SRtotalSupply = 2000;
        const SSRTotalSupply = 3000;
        const URTotalSupply = 5000;

        // 部署 奋斗狗NFT合约，需要参数，token全称，简称，URL
        const StruggleDogPauser = await ethers.getContractFactory("StruggleDog", OWNER);
        StruggleDog = await StruggleDogPauser.deploy(SRtotalSupply,SSRTotalSupply,URTotalSupply,"testDog", "Dog", "https://t721");


        // 部署随机数合约
        const randomContractPauser = await ethers.getContractFactory("Lottery", OWNER);
        Lottery = await upgrades.deployProxy(randomContractPauser, [10000], {initializer:"__Lottery_init"});
        await Lottery.deployed();

        // 部署盲盒合约
        const BlindBoxPauser = await ethers.getContractFactory("BlindBox", OWNER);
        BlindBox = await upgrades.deployProxy(BlindBoxPauser,["https://t1155", StruggleDog.address, Lottery.address], {initializer:'__BlindBox_init'});
        await BlindBox.deployed();

        // BlindBox 设置奋斗狗NFT minter
        var setMinter = await StruggleDog.connect(OWNER).setMinter(BlindBox.address);
        await setMinter.wait();

        var setBindBox = await Lottery.connect(OWNER).setBindBox(BlindBox.address);
        await setBindBox.wait();

        // ERC20 代币部署
        const ERC20PresetMinterPauser = await ethers.getContractFactory("ERC20PresetMinterPauser", OWNER);
        ERC20 = await ERC20PresetMinterPauser.deploy("TestERC20", "T20");

        // 部署盲盒商城合约
        const BBoxSwapPauser = await ethers.getContractFactory("BBoxSwap", OWNER);
        BBoxSwap = await upgrades.deployProxy(BBoxSwapPauser, ["0x8006C00F6217dE69f7F6630EEc377819CE68e37f", ERC20.address], {initializer: '__BBoxSwap_init'});  // 初始化ERC20
        await BBoxSwap.deployed();
    });


    it("Should be sellBox successful;", async function () {

        // 给地址铸造盲盒
        var mintNFTBOX1 = await BlindBox.connect(OWNER).mint(BBoxSwap.address, 1, 10000);
        await mintNFTBOX1.wait;
        //
        var mintNFTBOX2 = await BlindBox.connect(OWNER).mint(BBoxSwap.address, 2, 10000);
        await mintNFTBOX2.wait;

        // 上架
        var sellBox1 = await BBoxSwap.connect(OWNER).sellBox(1, 1, BlindBox.address, 1000, 10000, true, [ADDR1.address], [100]);
        await sellBox1.wait;

        // 上架
        var sellBox2 = await BBoxSwap.connect(OWNER).sellBox(2, 2, BlindBox.address, 1000, 10000, true, [ADDR1.address], [100]);
        await sellBox2.wait;

        // 上架
        var sellBox3 = await BBoxSwap.connect(OWNER).sellBox(3, 1, BlindBox.address, 1000, 10000, true, [ADDR1.address], [100]);
        await sellBox3.wait;

        // 上架
        var sellBox4 = await BBoxSwap.connect(OWNER).sellBox(4, 2, BlindBox.address, 1000, 10000, true, [ADDR1.address], [100]);
        await sellBox4.wait;


        var addBlindBoxRarityPR1 = await Lottery.connect(OWNER).addBlindBoxRarityPR(1,BlindBoxRarityPR1);
        await addBlindBoxRarityPR1.wait();

        var addBlindBoxRarityPR2 = await Lottery.connect(OWNER).addBlindBoxRarityPR(2,BlindBoxRarityPR2);
        await addBlindBoxRarityPR2.wait();



        // 购买
        var buyer1 = await BBoxSwap.connect(ADDR1).buyBbox(1, 100);
        await buyer1.wait;

        // 购买
        var buyer2 = await BBoxSwap.connect(ADDR1).buyBbox(2, 100);
        await buyer2.wait;

        // 购买
        var buyer3 = await BBoxSwap.connect(ADDR1).buyBbox(3, 100);
        await buyer3.wait;

        // 购买
        var buyer4 = await BBoxSwap.connect(ADDR1).buyBbox(4, 100);
        await buyer4.wait;

        // 开盲盒
        var openBbind1 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1.wait;

        // 开盲盒
        var openBbind2 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind2.wait;

        // 开盲盒
        var openBbind3 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind3.wait;

        // 开盲盒
        var openBbind4 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind4.wait;


        // 开盲盒
        var openBbind5 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind5.wait;

        // 开盲盒
        var openBbind6 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind6.wait;

        // 开盲盒
        var openBbind7 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind7.wait;

        // 开盲盒
        var openBbind8 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind8.wait;

        // 开盲盒
        var openBbind9 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind9.wait;

        // 开盲盒
        var openBbind10 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind10.wait;

        // 开盲盒
        var openBbind13 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind13.wait;

        // 开盲盒
        var openBbind15 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind15.wait;

        // 开盲盒
        var openBbind14 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind14.wait;

        // 开盲盒
        var openBbind11 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind11.wait;

        // 开盲盒
        var openBbind121 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind121.wait;

        // 开盲盒
        var openBbind122 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind122.wait;

        // 开盲盒
        var openBbind123 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind123.wait;

        // 开盲盒
        var openBbind1241 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1241.wait;

        // 开盲盒
        var openBbind1252 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1252.wait;
        // 开盲盒
        var openBbind1263 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1263.wait;
        // 开盲盒
        var openBbind1274 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1274.wait;
        // 开盲盒
        var openBbind1285 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1285.wait;
        // 开盲盒

        // 开盲盒
        var openBbind1246 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1246.wait;

        // 开盲盒
        var openBbind1257 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1257.wait;
        // 开盲盒
        var openBbind1268 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1268.wait;
        // 开盲盒
        var openBbind1279 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1279.wait;
        // 开盲盒
        var openBbind1218 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1218.wait;
        // 开盲盒

        // 开盲盒
        var openBbind124 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind124.wait;

        // 开盲盒
        var openBbind1251 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1251.wait;
        // 开盲盒
        var openBbind1261 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind1261.wait;
        // 开盲盒
        var openBbind1271 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind1271.wait;
        // 开盲盒
        var openBbind1281 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1281.wait;
        // 开盲盒
        var openBbind2129 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind2129.wait;


        // 开盲盒
        var openBbind1125 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1125.wait;
        // 开盲盒
        var openBbind12611 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind12611.wait;
        // 开盲盒
        var openBbind12711 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind12711.wait;
        // 开盲盒
        var openBbind1282 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1282.wait;
        // 开盲盒
        var openBbind1292 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind1292.wait;


        // 开盲盒
        var openBbind125 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind125.wait;
        // 开盲盒
        var openBbind126 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind126.wait;
        // 开盲盒
        var openBbind127 = await BlindBox.connect(ADDR1).burn(ADDR1.address,2,1);
        await openBbind127.wait;
        // 开盲盒
        var openBbind128 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind128.wait;
        // 开盲盒
        var openBbind129 = await BlindBox.connect(ADDR1).burn(ADDR1.address,1,1);
        await openBbind129.wait;

        var banlance = await StruggleDog.connect(ADDR1).balanceOf(ADDR1.address);
        console.log(banlance);

        var box11 = await BlindBox.connect(ADDR1).openMapRarity(1,1);
        var box12 = await BlindBox.connect(ADDR1).openMapRarity(1,2);
        var box13 = await BlindBox.connect(ADDR1).openMapRarity(1,3);

        console.log(box11, box12,box13);
        var box21 = await BlindBox.connect(ADDR1).openMapRarity(2,1);
        var box22 = await BlindBox.connect(ADDR1).openMapRarity(2,2);
        var box23 = await BlindBox.connect(ADDR1).openMapRarity(2,3);

        console.log(box21, box22, box23);

    });

});
